#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Version: Python3.7.4
Author: Simon
Site: https://gitee.com/simon4055
Time: 2019/08/08 22:00
"""

# coding = utf8
import requests
import itchat
from itchat.content import *
import time
import json
import datetime
import _thread
from apscheduler.schedulers.blocking import BlockingScheduler
import random
 
# 去图灵机器人官网注册后会生成一个apikey，可在个人中心查看
KEY = ['c54dbc108b11b8d103ca0601d34a3256','398ccadfdfc34794a222392ff25643f1']
# 个人位置信息
mapList = ['重庆电子工程职业学院(大学城东路76号)','2019-04-16 16:50:32']
# 群发消息
mesList = ['%s,\n好久都没联系了\n']
# 微信小号-管理 微信名称，微信备注，是否开启离线模式 ，离线默认回复
setting = ['AAAA','张嵩QQ95635748',1,"沉睡中..."]
# 特别关注
attention =['张嵩QQ95635748']
# 特别关注联系人的城市与coding
attention_city_coding = [['重庆','101040100'],['重庆','101040100'],['永州','101251401']]
# 定时问候语句(清晨,上午,中午,下午,傍晚,深夜,睡觉时间)
helloStr = [['早安','早上好',''],['上午好'],['中午好'],['下午好'],['吃晚饭了'],['快点睡觉了'],['睡觉时间']]
 
def get_response(msg):
    apiUrl = 'http://www.tuling123.com/openapi/api'
    data = {
        'key'   : KEY[random.randint(0, 4)],
        'info'   : msg,   # 这是要发送出去的信息
        'userid'  : 'XXXX',  #这里随意写点什么都行
    }
    try:
        # 发送一个post请求
        r = requests.post(apiUrl, data=data).json()
        # 获取文本信息，若没有‘Text’ 值，将返回Nonoe
        return r.get('text')
    except:
        return
# 通过定义装饰器加强函数 tuling_reply(msg) 功能，获取注册文本信息
@itchat.msg_register(itchat.content.TEXT)
def tuling_reply(msg):
    # 机器人离线模式下设置，用于机器人设置修改，例如：离线回复
    # 离线默认回复设置
    if '离线回复' in msg['Text'] and msg['User']['RemarkName'] == setting[1]:
        setting[3] = str(msg['Text'].replace('离线回复', ''))
        itchat.send("设置成功", msg['FromUserName'])
        return "离线默认回复已设置为:" + setting[3]
    # 激活机器人
    if '开启' in msg['Text'] and msg['User']['RemarkName'] == setting[1]:
        setting[2] = 1
        return "机器人当前状态：在线"
    # 离线模式
    if setting[2] == 0:
        itchat.send(setting[3], msg['FromUserName'])
        time.sleep(random.randint(2,6))
        return
    # 自定义关键词回复
    if msg['Text'] == '吴耀兵' or msg['Text'] == 'wybing' or msg['Text'] == 'wyb':
        return '这是小主人吖～～～'
    if '在吗' in msg['Text']:
        itchat.send("正在帮你找...", msg['FromUserName'])
        time.sleep(random.randint(2,6))
        itchat.send("主人当前不在线", msg['FromUserName'])
        time.sleep(random.randint(2,6))
        itchat.send("主人忙着学习呢", msg['FromUserName'])
        time.sleep(random.randint(2, 6))
        return '很高兴为您服务\nby 小兵机器人'
    if '机器人' in msg['Text'] or '小兵' in msg['Text']:
        itchat.send('Hi \n%s' % "你召唤了一只强大的机器人\n-来自调皮的机器人～～～", msg['FromUserName'])
        time.sleep(random.randint(2,6))
        return '很高兴为您服务\n发送"菜单"解锁更多功能\nby 小兵机器人'
    if '你好' in msg['Text']:
        itchat.send("很高兴为您服务～～～", msg['FromUserName'])
        time.sleep(random.randint(2, 6))
        return '发送"菜单"解锁更多功能\nby 小兵机器人'
    if msg['Text'] == '天气':
        itchat.send(get_weather(), msg['FromUserName'])
        return "你好, %s \n当前仅支持重庆天气，发送 城市+天气 获取具体城市天气，例如（北京天气）" % msg['User']['NickName']
    if '傻子' in msg['Text']:
        return "哼～\n你才是傻子\nby  小兵机器人"
    if '功能' == msg['Text']:
        itchat.send('Hi`%s' % "你召唤了一只强大的机器人\n-来自调皮的机器人～～～", msg['FromUserName'])
        time.sleep(random.randint(2,6))
        return '很高兴为您服务\nby 小兵机器人'
    if '彩蛋' == msg['Text']:
        itchat.send('Hi%s' % "\n恭喜发现彩蛋\n-来自调皮的机器人～～～", msg['FromUserName'])
        time.sleep(random.randint(2,6))
        return 'by 小兵机器人'
    # 功能回复
    # 位置
    if msg['Text'] == '位置' or msg['Text'] == "1":
        mapList[1]=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        return "主人位置:\n %s" % mapList[0] + "\n" + "更新时间:"+mapList[1]+"\nPS：强大的小兵等你发现～～～"
    # 男女比例
    if msg['Text'] == '男女比例' or msg['Text'] == "2":
        # 爬取自己好友的相关信息，返回一个好友列表
        friends = itchat.get_friends(update=True)[0:]
        # 初始化好友性别计数器
        male=female=other=0
        for i in friends[1:]:
            sex=i["Sex"]
            if sex == 1:
                male += 1
            elif sex == 2:
                female += 1
            else:
                other += 1
        # 好友总数
        total=len(friends[1:])
        # 性别统计
        return "男性好友：%.2f%%" % (float(male)/total*100)+"\n"+"女性好友：%.2f%%" % (float(female)/total*100)+"\n"+"未知性别：%.2f%%" % (float(other)/total*100)
               # +"\n"+"男性好友：%d(人)" % male+"\n"+"女性好友：%d(人)" % female+"\n"+"未知性别：%d(人)" % other +"\n"+"总计：%d(人)" % total
    # 手机号码
    if msg['Text'] == '手机号码' or msg['Text'] == "3":
        itchat.send("正在查询中...", msg['FromUserName'])
        time.sleep(random.randint(2, 6))
        itchat.send("手机号码:18875272019", msg['FromUserName'])
        time.sleep(random.randint(2, 6))
        itchat.send("网站:http://wybing.com", msg['FromUserName'])
        time.sleep(random.randint(2, 9))
        return "by 小兵机器人\n"
    #群发助手
    if ('#群发' in msg['Text'] or msg['Text'] == "4") and  msg['User']['RemarkName'] == setting[1]:
        if msg['User']['RemarkName'] == setting[1]:
            mesList[0] = str(msg['Text'].replace('#群发', ''))
            send_group()
            return "已启用群发功能,%s表示用户微信名称"
        itchat.send("群发功能已被关闭\n-来自调皮的机器人～～～", msg['FromUserName'])
        return '群发助手无法使用，仅供调试'
    # 特别关心
    if msg['Text'] == '特别关心' or msg['Text'] == "5":
        itchat.send('%s' % "对不起，主人说不允许你看\n-来自调皮的机器人～～～", msg['FromUserName'])
        return "%s ,\n主人未将你添加进特别关心列表！" % msg['User']['NickName']
    # 离线模式
    if '退出' in msg['Text'] and msg['User']['RemarkName'] == setting[1]:
        setting[2]=0
        return "机器人当前状态：离线"
    # 状态
    if '状态' in msg['Text']:
        str_state = ''
        if setting[2] == 0:
            str_state = "离线"
        else:
            str_state = "在线"
        return "机器人当前状态：" + str_state
    # 菜单
    if msg['Text'] == '菜单':
        itchat.send("===菜单===\n1.位置\n2.男女比例\n3.手机号码\n4.群发助手\n5.特别关心\n更多功能等你发现......\n       by 小兵机器人\n", msg['FromUserName'])
        time.sleep(random.randint(2, 6))
        itchat.send("回复 状态 获取机器人当前状态",msg['FromUserName'])
        time.sleep(random.randint(2, 6))
        return "群发助手和特别关心不对外开放"
    # 设置一个默认回复，在出现问题仍能正常回复信息
    defaultReply = '么么哒，无法识别消息: ' +msg['Text']
    reply = get_response(msg['Text'])
    # a or b 表示，如有a有内容，那么返回a，否则返回b
    return reply or defaultReply
 
# 处理其他文本类信息，包括位置、名片、通知、分享
@itchat.msg_register([MAP,CARD,NOTE,SHARING])
def text_reply(msg):
    if setting[2]==0 and msg['User']['RemarkName'] != setting[1]:
        return setting[3]
    if msg['Type'] == 'Map':
        # RemarkName 备注  NickName 微信昵称
        if msg['User']['RemarkName'] == setting[1]:
            mapList[0] = msg['Text']
            mapList[1] = (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(msg['CreateTime'])))
            itchat.send('%s\n%s\n%s\n' % ("成功录入系统", mapList[0],  mapList[1]), msg['FromUserName'])
        else:
            itchat.send('%s' % "你发的位置已经收到\n-来自调皮的机器人～～～", msg['FromUserName'])
    elif msg['Type'] == 'Card':
        # itchat.send('%s: %s' % (msg['Type'], msg['Text']), msg['FromUserName'])
        itchat.send('%s' % '分享的名片已经收到 by 小兵机器人', msg['FromUserName'])
        time.sleep(random.randint(2,6))
        itchat.send('%s' % '提示：此操作未做进一步处理', msg['FromUserName'])
    else:
        itchat.send('%s' % '404\n我会悄悄告诉主人的\nby 小兵机器人\n', msg['FromUserName'])
    itchat.send('%s: %s' % (msg['Type'], msg['Text']), msg['FromUserName'])
 
# 处理多媒体信息，包括图片、录音、文件、视频
@itchat.msg_register([PICTURE,RECORDING,ATTACHMENT,VIDEO])
def download_files(msg):
    # 离线模式
    if setting[2]==0:
        return setting[3]
    # msg['Text']是一个文件下载函数，传入文件名，将文件下载下来
    msg['Text'](msg['FileName'])
    # 把下载好的文件再发给发送者
    return '@%s@%s' % ({'Picture':'img','Video':'vid'}.get(msg['Type'],'fill'),msg['FileName'])
 
 
# 收到好友邀请自动添加好友
@itchat.msg_register(FRIENDS)
def add_friend(msg):
    itchat.add_friend(**msg['Text']) # 该操作会自动将新好友的消息录入，不需要重载通讯录
    itchat.send_msg('你好！', msg['RecommendInfo']['UserName'])
    time.sleep(random.randint(2,6))
    itchat.send_msg('小兵机器人很高兴为您服务!', msg['RecommendInfo']['UserName'])
 
 
# 处理群聊消息，在注册时增加isGroupChat=True 将判定为群聊回复
@itchat.msg_register(TEXT,isGroupChat=True)
def text_reply(msg):
    if msg['isAt']:
        itchat.send(u'@%s\u2005I received: %s' % (msg['ActualNickName'],msg['Content']), msg['FromUserName'])
 
# 获取天气
def get_weather(city='重庆',coding='101040100'):
    # 重庆 101040100
    url = 'http://t.weather.sojson.com/api/weather/city/'+coding
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0'}
    response = requests.get(url, headers=headers)
    data = json.loads(response.text)
 
    # 今日信息
    today = data['data']['forecast'][0]['ymd']
    today_hightem = data['data']['forecast'][0]['high']
    today_lowtem = data['data']['forecast'][0]['low']
    today_win = data['data']['forecast'][0]['fx'] + ' ' + data['data']['forecast'][0]['fl']
    today_wea = data['data']['forecast'][0]['type']
    notice = data['data']['forecast'][0]['notice']
 
    # 明日信息
    tomorrow = data['data']['forecast'][1]['ymd']
    tomorrow_hightem = data['data']['forecast'][1]['high']
    tomorrow_lowtem = data['data']['forecast'][1]['low']
    tomorrow_win = data['data']['forecast'][1]['fx'] + ' ' + data['data']['forecast'][1]['fl']
    tomorrow_wea = data['data']['forecast'][1]['type']
 
    today_data = '[' + today + ']' + '\n    ' + '温度：' + today_hightem + '/' + today_lowtem + '\n    ' + '天气：' + today_wea + '\n    ' + '风力：' + today_win + '\n\n'
    tomorrow_data = '[' + tomorrow + ']' + '\n    ' + '温度：' + tomorrow_hightem + '/' + tomorrow_lowtem + '\n    ' + '天气：' + tomorrow_wea + '\n    ' + '风力：' + tomorrow_win
    notice = '\n\n温馨提醒：' + notice
    info = [today_data, tomorrow_data, notice]
 
    return '【'+city+'天气】\n\n' + info[0] + info[1] + info[2]
 
def send_group():
    friendList = itchat.get_friends(update=True)[1:]  ###获取好友列表
    for friend in friendList:
        if friend['DisplayName'] == 'AAAA' or friend['NickName'] == 'AAAA':
            time.sleep(random.randint(15,100))
            if '%s' in mesList[0]:
                itchat.send(mesList[0] % (friend['DisplayName'] or friend['NickName']),toUserName = friend['UserName'])
                print(mesList[0] % (friend['DisplayName'] or friend['NickName']))
            else:
                itchat.send(mesList[0], toUserName = friend['UserName'])
                print(mesList[0])
# 根据时间发送问候语
def hello():
    # 获取问候的语句
    replystr=""
    # 是否发送天气
    weatherFlag = 0
    # 获取小时
    nowhour=datetime.datetime.now().hour
    # 清晨6-9点
    if nowhour>=6 and nowhour<9:
        replystr=helloStr[0][random.randint(0,len(helloStr[0])-1)]
        weatherFlag=1
    # 上午 9-12点
    elif nowhour>=9 and nowhour<12:
        replystr=helloStr[1][random.randint(0,len(helloStr[1])-1)]
    # 中午 12-15点
    elif nowhour >= 12 and nowhour < 15:
        replystr=helloStr[2][random.randint(0,len(helloStr[2])-1)]
    # 下午 15-18点
    elif nowhour >= 15 and nowhour < 18:
        replystr=helloStr[3][random.randint(0,len(helloStr[3])-1)]
    # 傍晚 18-21点
    elif nowhour >= 18 and nowhour < 21:
        replystr=helloStr[4][random.randint(0,len(helloStr[4])-1)]
    # 深夜 21-23点
    elif nowhour >= 21 and nowhour <= 23:
        replystr=helloStr[5][random.randint(0,len(helloStr[5])-1)]
    # 睡觉时间 0-6点
    else:
        replystr=helloStr[6][random.randint(0,len(helloStr[6])-1)]
 
    # 依照特别关心列表随机发送内容
    i = 0
    for name in attention:
        account = itchat.get_friends(name)  # 这里的aNickName是你给微信好友的备注
        for item in account:
            if item['RemarkName'] == name:  # 由于每次登录时朋友的UserName都会变，所以我们这样做
                user = item['UserName']  # 获取微信朋友的UserName
                # 给朋友发送信息
                if weatherFlag == 1:
                    itchat.send(get_weather(attention_city_coding[i][0],attention_city_coding[i][1]), toUserName=user)
                itchat.send(replystr, toUserName=user)
                time.sleep(random.randint(2,6))
        i+=1
 
# 使用热启动，不需要多次扫码
itchat.auto_login(hotReload=True)
# itchat.auto_login()
_thread.start_new_thread(itchat.run, ())
# 使用configured_reply方法
 
while 1:
    itchat.configured_reply()
    # some other functions
    sched = BlockingScheduler()
    sched.add_job(hello, 'cron', day_of_week='0-6', hour=7, minute=00)
    sched.start()
 
'''
# 未显式指定，那么则立即执行
sched.add_job(my_job, args=['text'])
# 在2009年11月6日执行
sched.add_job(my_job, 'date', run_date=date(2009, 11, 6), args=['text'])
# 在2009年11月6日 16:30:05执行
sched.add_job(my_job, 'date', run_date=datetime(2009, 11, 6, 16, 30, 5), args=['text']
sched.add_job(my_job, 'date', run_date='2009-11-06 16:30:05', args=['text'])
# 每2小时触发
sched.add_job(job_function, 'interval', hours=2)
# 周期触发的时间范围在2010-10-10 9:30 至 2014-06-15 11:00
sched.add_job(job_function, 'interval', hours=2, start_date='2010-10-10 09:30:00', end_date='2014-06-15 11:00:00')
'''
